﻿using ChiWalker.Dynamics;
using ChiWalker.Settings;
using ChiWalker.Spell;
using ChiWalker.Utilities;
using Styx;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace ChiWalker.Routines
{
    internal class Trinkets
    {
        private static readonly WoWItem Trinket1 = StyxWoW.Me.Inventory.Equipped.Trinket1;
        private static readonly WoWItem Trinket2 = StyxWoW.Me.Inventory.Equipped.Trinket2;

        [Behavior(BehaviorType.CombatBuffs)]
        [Spec(WoWSpec.MonkBrewmaster)]
        [Spec(WoWSpec.MonkWindwalker)]
        [Spec(WoWSpec.MonkMistweaver)]
        [Spec(WoWSpec.None)]
        [Class(WoWClass.Monk)]
        [Spec(WoWSpec.WarlockDestruction)]
        [Spec(WoWSpec.WarlockDemonology)]
        [Spec(WoWSpec.WarlockAffliction)]
        [Spec(WoWSpec.None)]
        [Class(WoWClass.Warlock)]
        [Spec(WoWSpec.DruidBalance)]
        [Spec(WoWSpec.DruidRestoration)]
        [Spec(WoWSpec.DruidGuardian)]
        [Spec(WoWSpec.DruidGuardian)]
        [Class(WoWClass.Druid)]
        [Spec(WoWSpec.WarriorArms)]
        [Spec(WoWSpec.WarriorFury)]
        [Spec(WoWSpec.WarriorProtection)]
        [Class(WoWClass.Warrior)]
        [Spec(WoWSpec.PriestShadow)]
        [Class(WoWClass.Priest)]
        [Spec(WoWSpec.PaladinProtection)]
        [Class(WoWClass.Paladin)]
        [Class(WoWClass.Rogue)]
        [Spec(WoWSpec.RogueCombat)]
        public static Composite TrinketRoutine()
        {
            return
                new PrioritySelector(
                    WaitFor.WaitForCast(),
                    new Decorator(
                        ret =>
                            Trinket1 != null && CanUseEquippedItem(Trinket1) &&
                            SettingsAnalyizer.AnalizeSpellUsage(ChiWalkerSettings.Instance.Trinket1),
                        new Action(ret =>
                        {
                            Trinket1.Use();
                            Logger.Write("Using {0}", Trinket1.Name);
                            return RunStatus.Failure;
                        }
                            )),
                    new Decorator(
                        ret =>
                            Trinket2 != null && CanUseEquippedItem(Trinket2) &&
                            SettingsAnalyizer.AnalizeSpellUsage(ChiWalkerSettings.Instance.Trinket2),
                        new Action(ret =>
                        {
                            Trinket2.Use();
                            Logger.Write("Using {0}", Trinket2.Name);
                            return RunStatus.Failure;
                        }
                            ))
                    );
        }

        private static bool CanUseEquippedItem(WoWItem item)
        {
            // Check for engineering tinkers!
            var itemSpell = Lua.GetReturnVal<string>("return GetItemSpell(" + item.Entry + ")", 0);
            if (string.IsNullOrEmpty(itemSpell)) return false;

            return item.Usable && item.Cooldown <= 0;
        }
    }
}